package 剑指offer;

public class Offer13_JZ66 {
    public static int movingCount(int threshold, int rows, int cols) {
        int[][] visited = new int[rows][cols];

        return slove(visited, threshold, rows, cols, 0, 0);
    }

    public static int slove(int[][] visited,int thrshold, int rows, int cols, int row, int col) {
        int count = 0;
        if (checkIn(visited, thrshold, rows, cols, row, col)) {
            visited[row][col] = 1;
            count = 1 + slove(visited, thrshold, rows, cols, row-1, col)
                    + slove(visited, thrshold, rows, cols, row+1, col)
                    + slove(visited, thrshold, rows, cols, row, col-1)
                    + slove(visited, thrshold, rows, cols, row, col-1);
        }
        return count;
    }

    public static boolean checkIn(int[][] visited, int threshold, int rows, int cols, int row, int col) {
        if (row>=0 && row<rows && col>=0 && col<cols && getDigital(threshold, row, col) && visited[row][col]==0) {
            return true;
        }
        return false;
    }

    public static boolean getDigital(int threshold, int row, int col) {
        int rowSum = 0, colSum = 0;
        while (row%10 != 0) {
            rowSum += row%10;
            rowSum = rowSum / 10;
        }
        while (col%10 != 0) {
            colSum += col%10;
            colSum = colSum / 10;
        }
        if (threshold >= (rowSum+colSum)) return true;
        return false;
    }

    public static void main(String[] args) {
        System.out.println(movingCount(5, 10, 10));
    }
}
